#!/bin/sh

EXEC="./synap"
#./synap ../tests/fibdec.txt ../tests/out.tmp < ../tests/test-scalarcopy.syn
#./synap ../tests/in5.txt ../tests/out.tmp < ../tests/test-copy5.syn
#Run "./synap" "../tests/in5.txt ../tests/out.tmp" "<" "../tests/test-copy5.syn"
#echo "args..."

# Run <args>
# Report the command, run it, and report any errors
Run() {
    echo "Begin Running \"$*\""
    #echo $* 1>&
    eval $* || {
	SignalError "$1 failed on $*"
	return 1
    }
    echo "Finished Running \"$1\""
}


# Compare <outfile> <reffile> <difffile>
# Compares the outfile with reffile.  Differences, if any, written to difffile
Compare() {
    generatedfiles="$generatedfiles $3"
    echo diff -b $1 $2 ">" $3 1>&2
    diff -b "$1" "$2" > "$3" 2>&1 || {
	SignalError "$1 differs"
	echo "FAILED $1 differs from $2" 1>&2
    }
}

Check() {
    error=0
    basename=`echo $1 | sed 's/.*\\///
                             s/.syn//'`
    reffile=`echo $1 | sed 's/.syn$//'`
    basedir="`echo $1 | sed 's/\/[^\/]*$//'`/."

    echo -n "$basename..."

    echo 1>&2
    echo "###### Testing $basename" 1>&2

    while read line
    do
	    ARGS=$line
	    break
    done < "../tests/test-scalarcopy.args"

    generatedfiles="${basename}.out" &&
    Run "$EXEC $ARGS" "<" $1 &&
    Compare ../tests/out.tmp ${reffile}.out ${basename}.out.diff

    # Report the status and clean up the generated files

    if [ $error -eq 0 ] ; then
	if [ $keep -eq 0 ] ; then
	    rm -f $generatedfiles
	fi
	echo "OK"
	echo "###### SUCCESS" 1>&2
    else
	echo "###### FAILED" 1>&2
	globalerror=$error
    fi
}


while read line
do 
	ARGS=$line
	break
done < "../tests/test-scalarcopy.args"

PROG="../tests/test-scalarcopy.syn"

Check $PROG
